package calculadora2;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTable;
import java.awt.Toolkit;
import java.awt.Font;
import javax.swing.JToggleButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TelaCalculadora extends JFrame {

	private double valor1 = 0.0,valor2 = 0.0;
	private String operacao;
	private JPanel contentPane;
	private JButton btnNum1;
	private JButton btnNum2;
	private JButton btnNum3;
	private JButton btnNum4;
	private JButton btnNum5;
	private JButton btnNum6;
	private JButton btnNum7;
	private JButton btnNum8;
	private JButton btnNum9;
	private JButton btnNum0;
	private JButton btnDiv;
	private JButton btnMult;
	private JButton btnSub;
	private JButton btnSoma;
	private JButton btnIgual;
	private JButton btnPonto;
	private JButton btnLimpar;
	private JButton btnRemoverNum;
	private JTextField txtMostraOperacao;
	private JLabel lblCalculadora;
	private JLabel lblNewLabel;
	private boolean ativar = false;
	private JLabel lblTextoOperacao;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaCalculadora frame = new TelaCalculadora();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TelaCalculadora() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(TelaCalculadora.class.getResource("/javax/swing/plaf/metal/icons/ocean/menu.gif")));
		setTitle("CALCULADORA");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 425, 481);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(51, 153, 0));
		contentPane.setForeground(new Color(255, 0, 51));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		btnNum1 = new JButton("1");
		btnNum1.setBackground(new Color(255, 255, 255));
		btnNum1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtMostraOperacao.setText(txtMostraOperacao.getText()+btnNum1.getText());
			}
		});
		btnNum1.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnNum1.setForeground(new Color(0, 0, 153));
		btnNum1.setBounds(10, 241, 91, 23);
		contentPane.add(btnNum1);
		
		btnNum2 = new JButton("2");
		btnNum2.setBackground(new Color(255, 255, 255));
		btnNum2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtMostraOperacao.setText(txtMostraOperacao.getText()+btnNum2.getText());
			}
		});
		btnNum2.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnNum2.setForeground(new Color(0, 0, 153));
		btnNum2.setBounds(118, 241, 91, 23);
		contentPane.add(btnNum2);
		
		btnNum3 = new JButton("3");
		btnNum3.setBackground(new Color(255, 255, 255));
		btnNum3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtMostraOperacao.setText(txtMostraOperacao.getText()+btnNum3.getText());
			}
		});
		btnNum3.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnNum3.setForeground(new Color(0, 0, 153));
		btnNum3.setBounds(219, 241, 91, 23);
		contentPane.add(btnNum3);
		
		btnNum4 = new JButton("4");
		btnNum4.setBackground(new Color(255, 255, 255));
		btnNum4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtMostraOperacao.setText(txtMostraOperacao.getText()+btnNum4.getText());
			}
		});
		btnNum4.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnNum4.setForeground(new Color(0, 0, 153));
		btnNum4.setBounds(10, 276, 91, 23);
		contentPane.add(btnNum4);
		
		btnNum5 = new JButton("5");
		btnNum5.setBackground(new Color(255, 255, 255));
		btnNum5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtMostraOperacao.setText(txtMostraOperacao.getText()+btnNum5.getText());
			}
		});
		btnNum5.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnNum5.setForeground(new Color(0, 0, 153));
		btnNum5.setBounds(118, 275, 91, 23);
		contentPane.add(btnNum5);
		
		btnNum6 = new JButton("6");
		btnNum6.setBackground(new Color(255, 255, 255));
		btnNum6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtMostraOperacao.setText(txtMostraOperacao.getText()+btnNum6.getText());
			}
		});
		btnNum6.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnNum6.setForeground(new Color(0, 0, 153));
		btnNum6.setBounds(219, 275, 91, 23);
		contentPane.add(btnNum6);
		
		btnNum7 = new JButton("7");
		btnNum7.setBackground(new Color(255, 255, 255));
		btnNum7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtMostraOperacao.setText(txtMostraOperacao.getText()+btnNum7.getText());
			}
		});
		btnNum7.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnNum7.setForeground(new Color(0, 0, 153));
		btnNum7.setBounds(10, 310, 91, 23);
		contentPane.add(btnNum7);
		
		btnNum8 = new JButton("8");
		btnNum8.setBackground(new Color(255, 255, 255));
		btnNum8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtMostraOperacao.setText(txtMostraOperacao.getText()+btnNum8.getText());	
			}
		});
		btnNum8.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnNum8.setForeground(new Color(0, 0, 153));
		btnNum8.setBounds(118, 309, 91, 23);
		contentPane.add(btnNum8);
		
		btnNum9 = new JButton("9");
		btnNum9.setBackground(new Color(255, 255, 255));
		btnNum9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtMostraOperacao.setText(txtMostraOperacao.getText()+btnNum9.getText());
			}
		});
		btnNum9.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnNum9.setForeground(new Color(0, 0, 153));
		btnNum9.setBounds(219, 309, 91, 23);
		contentPane.add(btnNum9);
		
		btnNum0 = new JButton("0");
		btnNum0.setBackground(new Color(255, 255, 255));
		btnNum0.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnNum0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtMostraOperacao.setText(txtMostraOperacao.getText()+btnNum0.getText());
			}
		});
		btnNum0.setForeground(new Color(0, 0, 153));
		btnNum0.setBounds(118, 343, 91, 23);
		contentPane.add(btnNum0);
		
		btnDiv = new JButton("/");
		btnDiv.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				valor1 = Double.valueOf(txtMostraOperacao.getText());
				operacao = btnDiv.getText();
				lblTextoOperacao.setText(txtMostraOperacao.getText()+operacao);
				btnPonto.setEnabled(true);
				txtMostraOperacao.setText("");
			}
		});
		btnDiv.setBackground(new Color(255, 255, 255));
		btnDiv.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnDiv.setForeground(new Color(0, 0, 153));
		btnDiv.setBounds(320, 241, 91, 23);
		contentPane.add(btnDiv);
		
		btnMult = new JButton("*");
		btnMult.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				valor1 = Double.valueOf(txtMostraOperacao.getText());
				operacao = btnMult.getText();
				lblTextoOperacao.setText(txtMostraOperacao.getText()+operacao);
				btnPonto.setEnabled(true);
				txtMostraOperacao.setText("");
			}
		});
		btnMult.setBackground(new Color(255, 255, 255));
		btnMult.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnMult.setForeground(new Color(0, 0, 153));
		btnMult.setBounds(320, 276, 91, 23);
		contentPane.add(btnMult);
		
		btnSub = new JButton("-");
		btnSub.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				valor1 = Double.valueOf(txtMostraOperacao.getText());
				operacao = btnSub.getText();
				lblTextoOperacao.setText(txtMostraOperacao.getText()+operacao);
				btnPonto.setEnabled(true);
				txtMostraOperacao.setText("");
			}
		});
		btnSub.setBackground(new Color(255, 255, 255));
		btnSub.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnSub.setForeground(new Color(0, 0, 153));
		btnSub.setBounds(320, 310, 91, 23);
		contentPane.add(btnSub);
		
		btnSoma = new JButton("+");
		btnSoma.setBackground(new Color(255, 255, 255));
		btnSoma.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				valor1 = Double.valueOf(txtMostraOperacao.getText());
				operacao = btnSoma.getText();				
				btnPonto.setEnabled(true);
				lblTextoOperacao.setText(txtMostraOperacao.getText()+operacao);
				txtMostraOperacao.setText("");
			}
		});
		btnSoma.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnSoma.setForeground(new Color(0, 0, 153));
		btnSoma.setBounds(320, 343, 91, 23);
		contentPane.add(btnSoma);
		
		btnIgual = new JButton("=");
		btnIgual.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				valor2 = Double.valueOf(txtMostraOperacao.getText());
				btnPonto.setEnabled(true);
				lblTextoOperacao.setText(lblTextoOperacao.getText()+txtMostraOperacao.getText()+"=");
				if(operacao.equals("+"))
					txtMostraOperacao.setText(String.valueOf(valor1+valor2));					
				
				else if(operacao.equals("-"))
						txtMostraOperacao.setText(String.valueOf(valor1-valor2));
					
				else if(operacao.equals("*"))
						txtMostraOperacao.setText(String.valueOf(valor1*valor2));
					
				else if(operacao.equals("/"))
						txtMostraOperacao.setText(String.valueOf(valor1/valor2));
				lblTextoOperacao.setText(lblTextoOperacao.getText()+txtMostraOperacao.getText());
				
			}
		});
		btnIgual.setBackground(new Color(255, 255, 255));
		btnIgual.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnIgual.setForeground(new Color(0, 0, 153));
		btnIgual.setBounds(219, 344, 91, 23);
		contentPane.add(btnIgual);
		
		btnPonto = new JButton(".");
		btnPonto.setBackground(new Color(255, 255, 255));
		btnPonto.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtMostraOperacao.setText(txtMostraOperacao.getText()+btnPonto.getText());
				btnPonto.setEnabled(false);
			}
		});
		btnPonto.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnPonto.setForeground(new Color(0, 0, 153));
		btnPonto.setBounds(10, 344, 91, 23);
		contentPane.add(btnPonto);
		
		btnLimpar = new JButton("LIMPAR");
		btnLimpar.setBackground(new Color(255, 255, 255));
		btnLimpar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtMostraOperacao.setText(" ");
				lblTextoOperacao.setText(" ");
				valor1 = valor2 = 0;
			}
		});
		btnLimpar.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnLimpar.setForeground(new Color(0, 0, 153));
		btnLimpar.setBounds(66, 207, 123, 23);
		contentPane.add(btnLimpar);
		
		btnRemoverNum = new JButton("<--");
		btnRemoverNum.setBackground(new Color(255, 255, 255));
		btnRemoverNum.setFont(new Font("Segoe Script", Font.PLAIN, 16));
		btnRemoverNum.setForeground(new Color(0, 0, 153));
		btnRemoverNum.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnRemoverNum.setBounds(232, 207, 117, 23);
		contentPane.add(btnRemoverNum);
		
		txtMostraOperacao = new JTextField();
		txtMostraOperacao.addMouseListener(new MouseAdapter() {	
			
		});
		txtMostraOperacao.setFont(new Font("Segoe Script", Font.PLAIN, 20));
		txtMostraOperacao.setEditable(false);
		txtMostraOperacao.setBounds(10, 139, 401, 52);
		contentPane.add(txtMostraOperacao);
		txtMostraOperacao.setColumns(10);
		
		lblCalculadora = new JLabel(" CALCULADORAS JAVE");
		lblCalculadora.setFont(new Font("Trajan Pro", Font.PLAIN, 20));
		lblCalculadora.setForeground(new Color(139, 0, 0));
		lblCalculadora.setBounds(82, 36, 241, 21);
		contentPane.add(lblCalculadora);
		
		lblNewLabel = new JLabel("Seus Problemas Acabaram.");
		lblNewLabel.setForeground(new Color(153, 0, 51));
		lblNewLabel.setFont(new Font("Segoe Script", Font.BOLD, 18));
		lblNewLabel.setBounds(134, 391, 283, 52);
		contentPane.add(lblNewLabel);
		
		JButton btnLigaDes = new JButton("Lig/Des");
		btnLigaDes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnSoma.setEnabled(ativar);
				btnSub.setEnabled(ativar);
				btnMult.setEnabled(ativar);
				btnDiv.setEnabled(ativar);
				btnNum1.setEnabled(ativar);
				btnNum2.setEnabled(ativar);
				btnNum3.setEnabled(ativar);
				btnNum4.setEnabled(ativar);
				btnNum5.setEnabled(ativar);
				btnNum6.setEnabled(ativar);
				btnNum7.setEnabled(ativar);
				btnNum8.setEnabled(ativar);
				btnNum9.setEnabled(ativar);
				btnNum0.setEnabled(ativar);
				btnPonto.setEnabled(ativar);
				btnIgual.setEnabled(ativar);
				btnLimpar.setEnabled(ativar);
				btnRemoverNum.setEnabled(ativar);
				if (ativar)	ativar=false;
				else ativar = true;
				txtMostraOperacao.setText("") ;
			}
		});
		btnLigaDes.setBackground(new Color(255, 102, 102));
		btnLigaDes.setBounds(10, 398, 91, 23);
		contentPane.add(btnLigaDes);
		
		lblTextoOperacao = new JLabel("");
		lblTextoOperacao.setFont(new Font("Segoe Script", Font.ITALIC, 20));
		lblTextoOperacao.setBounds(10, 81, 397, 47);
		contentPane.add(lblTextoOperacao);
	}
}
